/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.api.client.extensions.harmony.sample;

import com.google.api.client.extensions.harmony.sample.util.LogUtil;
import com.google.api.client.extensions.harmony.sample.util.ResUtil;
import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Key;

import java.util.List;

/**
 * Simple example for the <a href="http://www.dailymotion.com/doc/api/graph-api.html">Dailymotion
 * Graph API</a>.
 *
 * @author Yaniv Inbar
 */
public class DailyMotionSample {
    private static final String TAG = DailyMotionSample.class.getSimpleName();
    static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    static final JsonFactory JSON_FACTORY = new JacksonFactory();

    /** Represents a video feed. */
    public static class VideoFeed {
        @Key public List<Video> list;

        @Key("has_more")
        public boolean has_more;
    }

    /** Represents a video. */
    public static class Video {
        @Key public String id;

        @Key public String title;

        @Key public String channel;

        @Key public String owner;
    }

    /** URL for Dailymotion API. */
    public static class DailyMotionUrl extends GenericUrl {

        public DailyMotionUrl(String encodedUrl) {
            super(encodedUrl);
        }

        @Key public String fields;
    }

    public static String run(String testlink) throws Exception {
        String result = "";
        HttpRequestFactory requestFactory =
                HTTP_TRANSPORT.createRequestFactory(
                        request -> request.setParser(new JsonObjectParser(JSON_FACTORY)));
        DailyMotionUrl url = new DailyMotionUrl(testlink);
        url.fields = "id,title,channel,owner";
        HttpRequest request = requestFactory.buildGetRequest(url);
        VideoFeed videoFeed = request.execute().parseAs(VideoFeed.class);
        if (videoFeed.list.isEmpty()) {
            LogUtil.info(TAG, "No videos found.");
        } else {
            if (videoFeed.has_more) {
                LogUtil.info(TAG, "First ");
            }
            LogUtil.info(TAG, videoFeed.list.size() + " videos found:");
            for (Video video : videoFeed.list) {
                LogUtil.info(TAG, "-----------------------------------------------");
                result += "\n" +  "----------------------";
                LogUtil.info(TAG, "ID: " + video.id);
                result += "\n" +  "ID: " + video.id;
                LogUtil.info(TAG, "Title: " + video.title);
                result += "\n" +  "Title: " + video.title;
                LogUtil.info(TAG, "Channel: " + video.channel);
                result += "\n" +  "Channel: " + video.channel;
                LogUtil.info(TAG, "Owner: " + video.owner);
                result += "\n" +  "Owner: " + video.owner;
            }
        }
        return result;
    }
}